﻿using System;
using System.ComponentModel;
using System.Windows.Input;
using ZFCNew.Code;
using ZFCNew.Controls;
using ZFCNew.ZfcServiceReference;

namespace ZFCNew.ViewModels
{
    public class FeedbackModel : INotifyPropertyChanged
    {
        public event EventHandler CloseFeedback;
        public ICommand SubmitCommand { get; private set; }
        public ICommand CancelCommand { get; private set; }

        public Feedback Data { get; private set; }
        private readonly ServiceHelper _helper;
        public FeedbackModel()
        {
            SubmitCommand = new DelegateCommand(SubmitFeedback, (s) => true);
            CancelCommand = new DelegateCommand(CancelFeedback, (s) => true);
            Data = new Feedback();
            _helper = new ServiceHelper();
            _helper.FeedbackSent += HelperFeedbackSent;
        }

        void HelperFeedbackSent(object sender, DataEventArgs<int> e)
        {
            NotificationData message = null;
            if (e.IsSuccess)
            {
                message = new NotificationData
                              {
                                  Title = "Success!",
                                  Message = "Thank you for your feedback.",
                                  NotificationType = NotificationType.Success

                              };
            }
            else
            {
                message = new NotificationData
                {
                    Title = "Fail!",
                    Message = "The feedback cannot be saved this time. Please try again later.",
                    NotificationType = NotificationType.Error

                };
            }

            MainPage.NotificationPop.SetMessage(message);
            MainPage.NotificationPop.Show();
               


            Data = new Feedback();
        }

        private void SubmitFeedback(object s)
        {
            Data.UserInfo = Utility.ConvertToDbUser(FacebookHelper.LoggedInUser);
            _helper.SendFeedback(Data);
            if (CloseFeedback != null)
                CloseFeedback(this, EventArgs.Empty);
        }


        private void CancelFeedback(object s)
        {
            Data = new Feedback();
            if (CloseFeedback != null)
                CloseFeedback(this, EventArgs.Empty);
        }

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion
    }

    public class FeedbackData
    {
        public string FeedbackText { get; set; }
    }
}
